package com.ymatou.iapi.sellerproduct.testcase;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.parameter.ProdTplBean;
import com.ymatou.iapi.sellerproduct.service.GetProductAndCatalogInfoCall;
import com.ymatou.iapi.sellerproduct.service.SaveProductTemplateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_GetProductAndCatalogInfo {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	private static GetProductAndCatalogInfoCall getProductAndCatalogInfoCall = new GetProductAndCatalogInfoCall();
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("Get product detail");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductAndCatalogInfoCall.getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductAndCatalogInfo_001() {
		Logger.start(true, "Get product detail");
		try{
			SaveProductTemplateCall saveProductTemplateCall = new SaveProductTemplateCall();
			saveProductTemplateCall.deleteTemplate(sellerId);
			//	模版类型（1，尺码模版 2，买家须知 3，买手服务）
			ProdTplBean sizeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 1);
			ProdTplBean buyerNoticeTemplate = saveProductTemplateCall.saveTemplate(sellerId, 2);
			ProdTplBean sellerServiceTemplate = saveProductTemplateCall.saveTemplate(sellerId, 3);
			
			//get property 
			Map<String, Object> thirdCategoryAndPropertyVal = SellerProductCallService.getThirdCategoryAndPropertyValue();
			
			//发布商品
			JSONObject addProductBean = JsonBeanHelper.getJsonBean("com.ymatou.iapi.sellerproduct/AddNewSellerProduct/AddNewProductTpl.json");	
			Map<String, Object> updateMap = MapUtil.hashMap();
			updateMap.put("UserID", sellerId);
			updateMap.put("LoginID", sellerName);
			updateMap.put("Product->iThirdCategoryId", thirdCategoryAndPropertyVal.get("ThirdCategory"));
			updateMap.put("Product->PropertyList", (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			updateMap.put("Product->SizeTemplateId", sizeTemplate.getTemplateId());
			updateMap.put("Product->BuyerNoticeId",buyerNoticeTemplate.getTemplateId());
			updateMap.put("Product->SellerServiceId", sellerServiceTemplate.getTemplateId());	
			JsonBeanHelper.updateJsonBean(addProductBean, updateMap);
			
			String productId = SellerProductCallService.addNewProduct(addProductBean).getJSONObject("Data").getString("ProductID");
			
			//属性检查
			SellerProductCallService.checkProdProperty(productId,(JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			
			//模板检查
			//check product template
			List<ProdTplBean> prodTplBeanList = Arrays.asList(sizeTemplate,buyerNoticeTemplate,sellerServiceTemplate);
			SellerProductCallService.checkTemplateWithProd(productId,prodTplBeanList);
		
			
			JSONObject ret = getProductAndCatalogInfoCall.callAndGetReturnData(sellerId, productId);
			
			JSONObject productInfo = ret.getJSONObject("Data").getJSONObject("ProductAndCatalogInfoOnSellerProduct");
			
			Logger.verifyEquals(productInfo.getJSONObject("SizeTemplate").getInt("SizeTemplateId"), sizeTemplate.getTemplateId(), "商品信息 返回校验 sizeTemplate");
			Logger.verifyEquals(productInfo.getJSONObject("SizeTemplate").getString("Name"), sizeTemplate.getTemplateName(), "商品信息 返回校验 sizeTemplate");
			Logger.verifyEquals(productInfo.getJSONObject("BuyerNotice").getInt("BuyerNoticeId"), buyerNoticeTemplate.getTemplateId(), "商品信息 返回校验 BuyerNotice");
			Logger.verifyEquals(productInfo.getJSONObject("BuyerNotice").getString("Name"), buyerNoticeTemplate.getTemplateName(), "商品信息 返回校验 BuyerNotice");
			Logger.verifyEquals(productInfo.getJSONObject("SellerService").getInt("SellerServiceId"), sellerServiceTemplate.getTemplateId(), "商品信息 返回校验 SellerService");
			Logger.verifyEquals(productInfo.getJSONObject("SellerService").getString("Name"), sellerServiceTemplate.getTemplateName(), "商品信息 返回校验 SellerService");
			
			
			
			
			SellerProductCallService.checkPropertyList( productInfo.getJSONArray("PropertyList"), Integer.parseInt(thirdCategoryAndPropertyVal.get("ThirdCategory").toString()), (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList"));
			
			
			
		/*	List<SpuProperty>  spuPropertyList = new SpuPropertyWapper().selectByThirdcategoryidId(Integer.parseInt(thirdCategoryAndPropertyVal.get("ThirdCategory").toString()));
			JSONArray propertyList = productInfo.getJSONArray("PropertyList");
			Logger.verifyEquals(spuPropertyList.size(), propertyList.length(), "商品信息 返回校验 PropertyList record");
			
			for(int i=0;i<propertyList.length();i++){
				JSONObject propertyJson = propertyList.getJSONObject(i);
				Integer PropertyId = propertyJson.getInt("PropertyId");
				SpuProperty spuProperty = spuPropertyList.stream().filter(ele->ele.getPropertyId().equals(PropertyId)).findFirst().orElse(null);
				
				Logger.verifyNotNull(spuProperty, "spuProperty propertyid:"+PropertyId+" should be exists.");
				
				Map<String, Object> tgtMap = new HashMap<>();
				tgtMap.put("PropertyName", spuProperty.getName());
				tgtMap.put("PropertyValueType", spuProperty.getStyles());
				tgtMap.put("IsMultiple", spuProperty.getMultiSelect());
				
				//to do values check 
				propertyJson.remove("Values");
				AssertService.assertResultEqual(tgtMap, MapUtil.jsonToMap(propertyJson),"PropertyList id:"+ PropertyId+"check ");
		
				
				//property value check if has 
				JSONArray propertys = (JSONArray)thirdCategoryAndPropertyVal.get("PropertyList");
				boolean isHasVal = false;
				for(int j=0;j<propertys.length();j++){
					if (propertys.getJSONObject(j).getInt("PropertyId")==PropertyId){
						AssertService.checkArray((propertys.getJSONObject(j).getJSONArray("PropertyValue")),propertyJson.getJSONArray("InputValues"));
						isHasVal = true;
						break;
					}
				}
				
				
				if (!isHasVal){
					Logger.verifyEquals(0, propertyJson.getJSONArray("InputValues").length(), "InputValues should be empty");
				}
			}*/
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}
